package 多线程;

/*
    使用同步方法处理继承Thread类方式中的线程安全问题
 */
public class ThreadTest08 {
    public static void main(String[] args) {
        Window4 w1 = new Window4();
        Window4 w2 = new Window4();
        Window4 w3 = new Window4();

        w1.start();
        w2.start();
        w3.start();
    }
}

//卖票
class Window4 extends Thread{

    private static int ticket = 100;

    @Override
    public void run() {
        while (true){
            this.show();
        }
    }

    private static synchronized void show(){ //同步监视器为Window4.class
        //private synchronized void show(){ //此种方法是错误的
        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + ":" + Window4.ticket--);
        }
    }

}
